package com.study.web.user;

import com.study.vo.UserRegistVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @创建时间 2020/4/20 9:57
 */
@Slf4j
@Controller
public class LoginController {

    public String regist(@Valid UserRegistVo vo, BindingResult result, Model model) {
        if (result.hasErrors()) {



        /*    Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(fieldError -> {
                return fieldError.getField();
            }, fieldError -> {
                return fieldError.getDefaultMessage();
            }));*/
            //上述形式的简写
            Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            log.info(result.toString());
            model.addAllAttributes(collect);
            //校验出错
            return "forward:/index.html";
        }
        return "";
    }
}
